package com.example.smart_city_0829.activity.waimaidingcan;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.smart_city_0829.DeathData;
import com.example.smart_city_0829.R;
import com.example.smart_city_0829.base.BaseActivity;
import com.example.smart_city_0829.base.CommonAdapter;
import com.example.smart_city_0829.entity.waimai.WaiMaiBanner;
import com.example.smart_city_0829.entity.waimai.WaiMaiTheme;
import com.example.smart_city_0829.utils.Constant;
import com.example.smart_city_0829.view.MaxSizeGv;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.listener.OnBannerListener;

import java.util.stream.Collectors;

public class WaiMaiActivity extends BaseActivity {

    private Spinner spinner;
    private EditText search;
    private Banner banner;
    private MaxSizeGv gv;



    @Override
    protected int getLayoutId() {
        return R.layout.activity_wai_mai;
    }

    @Override
    protected void initView() {
        spinner = (Spinner) findViewById(R.id.spinner);
        search = (EditText) findViewById(R.id.search);
        banner = (Banner) findViewById(R.id.banner);
        gv = (MaxSizeGv) findViewById(R.id.gv);
    }

    @Override
    protected void initData() {
        WaiMaiBanner waiMaiBanner = DeathData.waiMaiBanner;
        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE_INSIDE)
                .setImageLoader(imageLoader)
                .setOnBannerListener(new OnBannerListener() {
                    @Override
                    public void OnBannerClick(int i) {
                        WaiMaiBanner.RowsBean rowsBean = waiMaiBanner.getRows().get(i);
                        Intent intent = new Intent(WaiMaiActivity.this, OrderDetailActivity.class);
                        intent.putExtra("id",rowsBean.getTargetId());
                        startActivity(intent);
                    }
                })
                .setImages(waiMaiBanner.getRows().stream().map(WaiMaiBanner.RowsBean::getAdvImg).collect(Collectors.toList()))
                .setBannerTitles(waiMaiBanner.getRows().stream().map(WaiMaiBanner.RowsBean::getAdvTitle).collect(Collectors.toList()))
                .start()
                .startAutoPlay();

        gv.setAdapter(new CommonAdapter<WaiMaiTheme.DataBean,CommonAdapter.CommGridViewHolder>(
                DeathData.waiMaiTheme.getData(),
                WaiMaiActivity.this
        ) {
            @Override
            protected void setData(WaiMaiTheme.DataBean data, int position, CommGridViewHolder viewHolder, View convertView, Context context) {
                Glide.with(context)
                        .load(Constant.BASE_URL2 + data.getImgUrl())
                        .into(viewHolder.im);
                viewHolder.serverName.setText(data.getThemeName());
            }

            @Override
            protected CommGridViewHolder getViewHolder() {
                return new CommGridViewHolder();
            }

            @Override
            protected void initView(CommGridViewHolder viewHolder, View convertView) {
                viewHolder.im = (ImageView) convertView.findViewById(R.id.im);
                viewHolder.serverName = (TextView) convertView.findViewById(R.id.server_name);
            }

            @Override
            protected int getItemLayoutResourceId() {
                return R.layout.server_list_item;
            }
        });
    }

    @Override
    protected void initEvent() {
        search.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH){
                    Intent intent = new Intent(WaiMaiActivity.this, SearchResultActivity.class);
                    if (spinner.getSelectedItemPosition() == 0){
                        //店家
                        intent.putExtra("type",0);
                    }else {
                        //美食
                        intent.putExtra("type",1);
                    }
                    intent.putExtra("key",search.getText().toString());
                    startActivity(intent);
                    return true;
                }
                return false;
            }
        });

        gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(WaiMaiActivity.this, OrderListActivity.class);
                intent.putExtra("position",position);
                startActivity(intent);
            }
        });
    }

    @Override
    protected String getActionBarTitle() {
        return "外卖订餐";
    }
}